﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SnmpForm.APConfig
{
    public partial class ucWlanAdvance : ucAPConfig
    {
        public ucWlanAdvance()
        {
            InitializeComponent();
        }
        public override void LoadAp()
        {
            comboBoxPower.DataSource = AP_DEFINE_DATA.WIRELESS_POWER;
            Update();
            //
            bool bPrivilege = BusinessWrapper.User.Instance.Permision(
                BusinessWrapper.Types.Privilege.ManageAP.Wlan.WlanAdvance.Save.permision);
            buttonSave.Visible = bPrivilege;
        }

        public override bool Update(bool bView = true)
        {
            DbWrapper.DbDataTypes.WirelessInfo info = m_ap.GetApWireless();
            if (bView)
            {
                //
                int nItemIndex = 0;
                ApConfigItem item = AP_DEFINE_DATA.find(AP_DEFINE_DATA.WIRELESS_POWER,
                    info.power, out nItemIndex);
                if (item != null)
                {
                    comboBoxPower.SelectedIndex = nItemIndex;
                }

                checkBoxWMM.Checked = info.wmm;
                checkBoxGI.Checked = info.short_gi;
            }
            else
            {
                ApConfigItem item = (ApConfigItem)comboBoxPower.SelectedValue;
                info.power = (int)item.Value;

                info.wmm = checkBoxWMM.Checked;
                info.short_gi = checkBoxGI.Checked;
            }
            return true;
        }

        public override bool SyncToApCommit()
        {
            DbWrapper.DbDataTypes.WirelessInfo oldinfo, newinfo;
            oldinfo = m_ap.GetApWireless().Clone();
            Update(false);
            newinfo = m_ap.GetApWireless();
            bool bSuccess = true;
            if (oldinfo.power != newinfo.power)
            {
                bSuccess = 
                    m_ap.SetTxPower(newinfo.power);
                if (!bSuccess)
                    return bSuccess;
            }
            if(oldinfo.wmm != newinfo.wmm){
                bSuccess = m_ap.SetWMM(newinfo.wmm);
                if (!bSuccess)
                {
                    return bSuccess;
                }
            }
            if (oldinfo.short_gi != newinfo.short_gi)
            {
                bSuccess = m_ap.SetShortGI(newinfo.short_gi);
                if (!bSuccess)
                {
                    return bSuccess;
                }
            }
            return bSuccess;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (SyncToAp())
            {
                if (SaveToDb())
                {
                    NoticeConfigFormClose();
                }
            }
        }
    }
}
